package api

import (
	"context"
	"fiberdemo/app/model"
	"fiberdemo/app/service"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"github.com/qiniu/qmgo"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"gopkg.in/mgo.v2/bson"
	//"go.mongodb.org/mongo-driver/bson"
	"log"
	"strconv"
)

var Sky = newSkyApi()

type skyApi struct {
}
type Test struct {
	QueryResultList string
}
func newSkyApi() *skyApi {
	return &skyApi{}
}

func (a *skyApi) Index(c *fiber.Ctx) error {
	p := new(model.Nlps)
	if err := c.BodyParser(p); err != nil {
		fmt.Println(err)
		return err
	}

	x:= service.SearchNlp(p.Name,p.Top,p.Tab)

	newMap := map[int]interface{}{}

	ctx:=context.Background()
	cli,err2:=qmgo.Open(ctx,&qmgo.Config{Uri:"mongodb://admin:pp888888@m2.moono.vip:27017",Database:"myapp",Coll:"engines"})
	if  err2 != nil {
		log.Println(err2)
		return err2
	}
	for key, value := range x.QueryResultList[0].Ids {
		one:=new(model.Engines)
		string:=strconv.FormatInt(value,10)
		err:=cli.Find(ctx, bson.M{"myid": string}).One(&one)
		if err != nil {
			// log.Println(err)
		}else{
			newMap[key]=one
		}
	}

	defer func(){
		if err:=cli.Close(ctx);err!=nil{
			panic(err)
		}
	}()

	return c.JSON(newMap)

}


func (a *skyApi) Change(c *fiber.Ctx) error {
	//1通过id去mongodb查询nlp数据库的id
	ctx:=context.Background()
	one:=new(model.Engines)
	p := new(model.Engines)
	if err := c.BodyParser(p); err != nil {
		fmt.Println(err)
		return err
	}
    tid, _ :=primitive.ObjectIDFromHex(p.Id)
	cli,_:=qmgo.Open(ctx,&qmgo.Config{Uri:"mongodb://admin:pp888888@m2.moono.vip:27017",Database:"myapp",Coll:"engines"})
	err:=cli.Find(ctx, bson.M{"_id":tid}).One(&one)

	if err != nil {
		fmt.Println(err)
		return err
	}
	defer func(){
		if err:=cli.Close(ctx);err!=nil{
			panic(err)
		}
	}()
	nlpid:=one.Id
    //2拿到id后去删除
	cab2:=service.Nlpdelete(nlpid)
	//3更新mongodb的数据

	if(cab2=="success"){
		err3 := cli.UpdateOne(ctx, bson.M{"_id":tid}, bson.M{"$set": bson.M{"Menu":p.Menu,"NewsName":p.NewsName,"Content":p.Content,"myid": p.Myid}})
		if err3 != nil {
			log.Println(err3)
			return err3
		}
	}
	return c.JSON("ok")
}

func (a *skyApi) Check(c *fiber.Ctx) error {
 //1去数据库遍历除所有的myid
	ctx:=context.Background()
	one:=[]model.Engines{}

	cli,_:=qmgo.Open(ctx,&qmgo.Config{Uri:"mongodb://admin:pp888888@m2.moono.vip:27017",Database:"myapp",Coll:"engines"})
	err:=cli.Find(ctx, bson.M{"Type":"cms"}).Select(bson.M{"myid": 1}).All(&one)
	if  err!= nil {
		fmt.Println(err)
	}
	//2.遍历去删除这些id
	//2.1遍历除mongo中的ids
	saveids:=make([]int64,0)
	for _, value := range one{
		iu, err := strconv.ParseInt(value.Myid, 10, 64)
		if err != nil {
			fmt.Println(err)
		}else{
			saveids=append(saveids,iu)
		}
	}

	//2.2获取nlp中的ids

	cab:=service.SearchNlp("","10000","tab1")
	res := make([]int64, 0)
	for _, value := range cab.QueryResultList[0].Ids{
		if !inslice(value, saveids) {
			res = append(res, value)
		}
	}
	if(service.Nlpidsdelete(res)!="success"){
		return c.JSON("失败")
	}
	return c.JSON("去除了多余的数据")
}

func inslice(n int64, h []int64) bool {
	for _, v := range h {
		if v == n {
			return true
		}
	}
	return false
}
